package cn.stronglink.asset.module.assetmanage.borrowreturn.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.BorrowReturn;
import cn.stronglink.asset.model.BorrowReturnDetail;
import cn.stronglink.asset.module.assetmanage.borrowreturn.service.BorrowReturnService;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;

/**
 * @author bmc
 * 2017-12-03
 * 
 * */
@Controller
@RequestMapping(value="borrowReturn")
public class BorrowReturnController extends AbstractController{
	@Autowired
	private BorrowReturnService borrowReturnService;
	
	/**
	 * 查询迁移单列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value="queryBorrowReturnList")
	public ResponseEntity<ModelMap> queryBorrowReturnList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String,Object> params){
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("operatorBy", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<BorrowReturn> page = (Page<BorrowReturn>) super.getPage(params);
		Page<BorrowReturn> data = borrowReturnService.queryBorrowReturnList(page,params);
		return setSuccessModelMap(modelMap,data);
	}
	
	/**
	 * 移除
	 * 注意:当借用单为新建时可以删除，其他状态不可删
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 */
	@OperateLog(module = "借用归还",desc="删除借用归还单", type = OpType.DEL)
	@RequestMapping(value="remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id){
		borrowReturnService.remove(id);
		return setSuccessModelMap(modelMap,null);
	}
	
	/**
	 * 插入借用单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param borrowReturn
	 */
	@OperateLog(module = "借用归还",desc="添加借用归还单", type = OpType.ADD)
	@RequestMapping(value="create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody BorrowReturn borrowReturn) {
		borrowReturnService.create(borrowReturn);
		return setSuccessModelMap(modelMap,null);
	}
	
	/**
	 * 通过ID查询借用单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(value="qryById")
	public ResponseEntity<ModelMap> qryById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id){
		BorrowReturn data= borrowReturnService.qryById(id);
		return setSuccessModelMap(modelMap,data);
	}
	
	/**
	 * 修改借用单
	 * 注意:完成状态的借用单不可修改
	 * 		新建状态可改
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param borrowReturn
	 * 		
	 */
	@OperateLog(module = "借用归还",desc="修改借用归还单", type = OpType.UPDATE)
	@RequestMapping(value="edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody BorrowReturn borrowReturn){
		borrowReturnService.edit(borrowReturn);
		return setSuccessModelMap(modelMap,null);
	}
	
	/**
	 * 查询借用单详情列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value="queryBorrowReturnDetailList")
	public ResponseEntity<ModelMap> queryBorrowReturnDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String,Object> params){
		@SuppressWarnings("unchecked")
		Page<BorrowReturnDetail> page = (Page<BorrowReturnDetail>) super.getPage(params);
		Page<BorrowReturnDetail> data = borrowReturnService.queryBorrowReturnDetailList(page,params);
		return setSuccessModelMap(modelMap,data);
	}
	
	/**
	 * 通过borrowReturnId查询借用单详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryDetailById")
	public ResponseEntity<ModelMap> queryBorrowReturnDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id){
		BorrowReturnDetail data = borrowReturnService.queryDetailById(id);
		return setSuccessModelMap(modelMap,data);
	}
	
	/**
	 * 确认借用
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "借用归还",desc="确认借用", type = OpType.UPDATE)
	@RequestMapping(value="updateToBorrow")
	public ResponseEntity<ModelMap> updateToBorrow(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody  BorrowReturn borrowReturn){
		borrowReturnService.updateToBorrow(borrowReturn);
		return setSuccessModelMap(modelMap,null);
	}
	
	/**
	 * 确认归还
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "借用归还",desc="确认归还", type = OpType.UPDATE)
	@RequestMapping(value="updateToReturn")
	public ResponseEntity<ModelMap> updateToReturn(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody  BorrowReturn borrowReturn){
		borrowReturnService.updateToReturn(borrowReturn);
		return setSuccessModelMap(modelMap,null);
	}
	
	/**
	 * 所在机房,设备类型,品牌,设备名称模糊查找资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "querAssetListByParam")
	public ResponseEntity<ModelMap> queryDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {	
		@SuppressWarnings("unchecked")
		Page<ItAssetVo> page = (Page<ItAssetVo>) super.getPage(params);
		Page<ItAssetVo> data = borrowReturnService.querAssetListByParam(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 通过ID查询未归还列表单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(value="qryBorrowListById")
	public ResponseEntity<ModelMap> qryBorrowListById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id){
		BorrowReturn data= borrowReturnService.qryBorrowListById(id);
		return setSuccessModelMap(modelMap,data);
	}
}
